package com.fg.pro.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fg.pro.model.exception.CustomException;
import com.fg.pro.model.sys.dto.SysUserDto;
import com.fg.pro.model.sys.entity.SysDept;
import com.fg.pro.model.sys.entity.SysMenu;
import com.fg.pro.model.sys.entity.SysUserRole;
import com.fg.pro.model.utils.AppJwtUtil;
import com.fg.pro.model.utils.MD5Utils;
import com.fg.pro.model.utils.UserIdThread;
import com.fg.pro.sys.dao.SysUserDao;
import com.fg.pro.model.sys.entity.SysUser;
import com.fg.pro.sys.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.fg.pro.model.enums.AppHttpCodeEnum.USER_ERROR_LOGIN;
import static com.fg.pro.model.enums.AppHttpCodeEnum.USER_ERROR_PASSWORD;

/**
 * 用户(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2025-07-17 10:10:47
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private SysDeptService sysDeptService;

    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize, String userName) {
        PageHelper.startPage(pageNum, pageSize);
//        List<SysUser> list = query().eq(StrUtil.isNotBlank(userName),"user_name", userName).orderByDesc("create_time").list();
        List<SysUser> list = this.baseMapper.getList(userName);
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<SysMenu> getMenuTreeById() {
        Long userId = UserIdThread.sysUserId();
        SysUserRole userRole = sysUserRoleService.query().eq("user_id", userId).one();
        List<Long> menuList = sysRoleMenuService.query().eq("role_id", userRole.getRoleId()).list().stream().map(item -> item.getMenuId()).collect(Collectors.toList());
        List<SysMenu> firList = sysMenuService.query().in("id", menuList).eq("menu_lev", 1).eq("menu_show", 1).orderByAsc("menu_sort").list();
        for (SysMenu sysMenu : firList) {
            List<SysMenu> secList = sysMenuService.query().in("id", menuList).eq("menu_parent_id", sysMenu.getId()).eq("menu_lev", 2).eq("menu_show", 1).orderByAsc("menu_sort").list();
            if (!secList.isEmpty()) {
                sysMenu.setChildren(secList);
            }
        }
        return firList;
    }

    @Override
    public SysUserDto login(String userAccount, String userPassword) {
        SysUser user = query().eq("user_account", userAccount.trim()).or().eq("user_tel", userAccount.trim()).one();
        if (user == null) {
            throw new CustomException(USER_ERROR_LOGIN);
        }
        userPassword = MD5Utils.encode(userPassword);
        if (!userPassword.equals(user.getUserPassword())) {
            throw new CustomException(USER_ERROR_PASSWORD);
        }
        SysUserDto userDto = BeanUtil.copyProperties(user, SysUserDto.class);
        String token = AppJwtUtil.getAdminToken(user.getId().longValue());
        userDto.setToken(token);
        return userDto;
    }

    @Override
    public SysUserDto getSysUserDtoById(String id) {
        SysUser user = getById(id);
        List<String> menuStrList = this.baseMapper.getMenuStrListById(id);
        SysUserDto sysUserDto = BeanUtil.copyProperties(user, SysUserDto.class);
        sysUserDto.setMenuStrList(menuStrList);
        return sysUserDto;
    }

    @Override
    public SysUser getUserById(String id) {
        SysUser sysUser = getById(id);
        SysDept sysDept = sysDeptService.getById(sysUser.getDeptId());
        sysUser.setDeptName(sysDept.getDeptName());
        return sysUser;
    }

}

